package org.de.maeyer.sampler;

import static java.util.Arrays.*;
import static java.lang.Double.*;

/**
 * @author Peter De Maeyer
 */
public class AutoCategorizingDoubleHistogramAccumulator extends DoubleHistogramAccumulator {
	
	private int modeIndex;
	
	private double modeDensity;

	public AutoCategorizingDoubleHistogramAccumulator(double[] bounds, long[] counts) {
	    super(bounds, counts);
	    for (int i = 1, n = bounds.length; i < n; i++) {
	    	double density = counts[i] / (bounds[i] - bounds[i - 1]);
	    	if (density > modeDensity) {
	    		modeIndex = i;
	    		modeDensity = density;
	    	}
	    }
    }

	public AutoCategorizingDoubleHistogramAccumulator(double[] bounds) {
	    super(bounds, new long[bounds.length + 1]);
    }
	
	@Override
	public void accumulateDouble(double value) {
		int index = binarySearch(bounds, value);
		if (index >= 0) {
			counts[index + 1]++;
		} else {
			counts[~index]++;
		}
	}
}
